package com.hqyj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.DepotJacketMapper;
import com.hqyj.pojo.DepotJacket;
import com.hqyj.service.DepotJacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class DepotJacketServiceImpl implements DepotJacketService {

    //注入mapper
    @Autowired(required = false)
    DepotJacketMapper depotJacketMapper;


    @Override
    public HashMap<String, Object> select(DepotJacket d) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<DepotJacket> page = new Page<>(d.getPage(),d.getRow());

        //条件构造器
        QueryWrapper<DepotJacket> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(d.getJacketId()!=null,"jacket_id",d.getJacketId());
        queryWrapper.like(d.getJacketName()!=null&&!d.getJacketName().equals(""),"department_jacket_name",d.getJacketName());

        //查询数据库
        Page<DepotJacket> dList =depotJacketMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",dList.getRecords());
        //总页数
        map.put("totalPage",dList.getPages());
        //总条数
        map.put("total",dList.getTotal());
        //当前页
        map.put("curPage",d.getPage());
        if(d.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",d.getPage()-1);

        }
        if(d.getPage()==dList.getPages()){
            //下一页
            map.put("nextPage",dList.getPages());

        }else{
            map.put("nextPage",d.getPage()+1);

        }
        //每页显示条数
        map.put("row",d.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(DepotJacket d) {
        HashMap<String, Object> map = new HashMap<>();
        int num = depotJacketMapper.insert(d);
        if (num > 0) {
            map.put("info", "保存成功");
        }else {
            map.put("info", "保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(DepotJacket d) {
        HashMap<String, Object> map = new HashMap<>();
        int num = depotJacketMapper.updateById(d);
        if (num > 0) {
            map.put("info", "保存成功");
        }else {
            map.put("info", "保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer d) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num =depotJacketMapper.deleteById(d);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }
}


